package com.zx.bus.controller;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.zx.bus.domain.Car;
import com.zx.bus.service.CarService;
import com.zx.bus.vo.CarVo;
import com.zx.sys.constast.SysConstast;
import com.zx.sys.utils.AppFileUtils;
import com.zx.sys.utils.DataGridView;
import com.zx.sys.utils.ResultObj;

@RestController
@RequestMapping("car")
public class CarController {
	
	@Autowired
	private CarService carService;
	
	@RequestMapping("loadAllCar")
	public DataGridView loadAllCar(CarVo carVo) {
		return this.carService.queryAllCar(carVo);
	}
	
	@RequestMapping("addCar")
	public ResultObj addCar(CarVo carVo) {
		try {
			if(!carVo.getCarimg().equals(SysConstast.DEFAULT_CAR_IMG)) {
				String filePath=AppFileUtils.updateFileName(carVo.getCarimg(),SysConstast.FILE_UPLOAD_TEMP);
				carVo.setCarimg(filePath);
			}
			
			carVo.setCreatetime(new Date());
			this.carService.addCar(carVo);
			return new ResultObj(SysConstast.CODE_SUCCESS, SysConstast.ADD_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			return new ResultObj(SysConstast.CODE_ERROR, SysConstast.ADD_ERROR);
		}
	}
	
	@RequestMapping("updateCar")
	public ResultObj updateCar(CarVo carVo) {
		try {
			String carimg = carVo.getCarimg();
			if(carimg.endsWith(SysConstast.FILE_UPLOAD_TEMP)) {
				String filePath=AppFileUtils.updateFileName(carVo.getCarimg(),SysConstast.FILE_UPLOAD_TEMP);
				carVo.setCarimg(filePath);
				
				Car car=this.carService.queryCarByCarNumber(carVo.getCarnumber());
				AppFileUtils.removeFileByPath(car.getCarimg());
			}
			
			this.carService.updateCar(carVo);
			return new ResultObj(SysConstast.CODE_SUCCESS, SysConstast.UPDATE_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			return new ResultObj(SysConstast.CODE_ERROR, SysConstast.UPDATE_ERROR);
		}
	}
	
	@RequestMapping("deleteCar")
	public ResultObj deleteCar(CarVo carVo) {
		try {
			Car car = carService.queryCarByCarNumber(carVo.getCarnumber());
			if(!car.getCarimg().equals(SysConstast.DEFAULT_CAR_IMG)){
				AppFileUtils.removeFileByPath(car.getCarimg());
			}
			
			this.carService.deleteCar(carVo.getCarnumber());
			return new ResultObj(SysConstast.CODE_SUCCESS,SysConstast.DELETE_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			return new ResultObj(SysConstast.CODE_ERROR,SysConstast.DELETE_ERROR);
		}
	}

	@RequestMapping("deleteBatchCar")
	public ResultObj deleteBatchCar(CarVo carVo) {
		try {
			for (String id : carVo.getIds()) {
				Car car = carService.queryCarByCarNumber(id);
				if(!car.getCarimg().equals(SysConstast.DEFAULT_CAR_IMG)){
					AppFileUtils.removeFileByPath(car.getCarimg());
				}
			}
			
			this.carService.deleteBatchCar(carVo.getIds());
			return new ResultObj(SysConstast.CODE_SUCCESS,SysConstast.DELETE_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			return new ResultObj(SysConstast.CODE_ERROR,SysConstast.DELETE_ERROR);
		}
	}
}
